﻿using UnityEditor;
#if UNITY_EDITOR 
/// <summary>
/// 编辑器窗口绘制类型
/// </summary>
public enum enESF_EditorWindowDrawType
{ 
    /// <summary>
    /// Unity
    /// </summary>
    Unity,
    /// <summary>
    /// Odin
    /// </summary>
    Odin
}
/// <summary>
/// 抽象编辑器窗口
/// </summary>
public abstract class AbsESF_EditorWindow : EditorWindow
{
    /// <summary>
    /// 配置设定
    /// </summary>
    protected abstract AbsESF_EditorBuildAsset config { get; }

    /// <summary>
    /// 窗口绘制类型
    /// </summary>
    protected virtual enESF_EditorWindowDrawType drawType => enESF_EditorWindowDrawType.Unity;

    /// <summary>
    /// OnLostFocus
    /// </summary>
    protected virtual void OnLostFocus()
    {
        OnSaveConfig();
    }

    private void OnDestroy()
    {
        OnSaveConfig();
        OnRunDestroy();
    }
 
    
    /// <summary>
    /// OnRunDestroy
    /// </summary>
    protected virtual void OnRunDestroy() { }

    /// <summary>
    /// 保存配置
    /// </summary>
    /// <returns>true:保存成功,false:保存失败</returns>
    protected bool OnSaveConfig()
    {
        bool? rst = config?.SaveAsset();
        return rst.HasValue ? rst.Value : true;
    }

    private void OnGUI()
    {
        OnBeforeRunGUI();
        config?.DrawGUI();
        OnRunGUI();
        switch (drawType)
        {
            case enESF_EditorWindowDrawType.Unity:
                OnDrawBottomUnityGUI();
                break;
            case enESF_EditorWindowDrawType.Odin:
                OnDrawBottomOdinGUI();
                break;
        }
    }

    /// <summary>
    /// OnBeforeRunGUI
    /// </summary>
    protected virtual void OnBeforeRunGUI() { }

    /// <summary>
    /// OnRunGUI
    /// </summary>
    protected virtual void OnRunGUI() { }

    /// <summary>
    /// 底部绘制UnityGUI
    /// </summary>
    protected virtual void OnDrawBottomUnityGUI() { }

    /// <summary>
    /// 底部绘制Odin
    /// </summary>
    protected virtual void OnDrawBottomOdinGUI(){}
}
#endif